package com.aiflow.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.aiflow.bean.User;
import com.aiflow.db.ConnDB;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomSendAction extends ActionSupport implements RequestAware,
		ServletResponseAware {

	private Map req;
	private HttpServletResponse resp;
	ConnDB db = new ConnDB();

	public void findCustomSend() {
		HttpServletRequest req = ServletActionContext.getRequest();
		int start = Integer.parseInt(req.getParameter("start"));
		int limit = Integer.parseInt(req.getParameter("limit"));
		int end = start + limit;
		int begin = start + 1;
		String startTime = (String) req.getParameter("startTime");
		String endTime = (String) req.getParameter("endTime");
		if (startTime != null && !startTime.equals("")) {
			startTime = (startTime.split("T"))[0];
		}
		if (endTime != null && !endTime.equals("")) {
			endTime = (endTime.split("T"))[0];
		}

		ConnDB db = new ConnDB();
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String whereSql = "";
		if (startTime != null && !startTime.equals("")) {
			whereSql += " and   c.addTime >= '" + startTime + "' ";
		}
		if (endTime != null && !endTime.equals("")) {
			whereSql += " and  c.addTime < '" + endTime + "' ";
		}

		String sqlCount = "";
		Connection con = db.getConnection();
		PreparedStatement ps = null;

		try {
			String sql = "select c.id,c.tele,c.context,c.addTime,u.name,(row_number() over(ORDER BY c.id desc)) rb  from  t_s_customSend c,dbo.t_u_user_Info u where c.sendUser = u.id "
					+ whereSql;

			String sqlCon = "select * from " + "(" + sql + " ) tt"
					+ " where tt.rb between " + begin + " and " + end;

			sqlCount = "select count(1)  from (" + sql + ") aa";

			ps = con.prepareStatement(sqlCon);
			rs = ps.executeQuery();

			while (rs.next()) {
				Map<String, String> mapdata = new HashMap<String, String>();
				mapdata.put("id", rs.getString(1));
				mapdata.put("tele", rs.getString(2));
				mapdata.put("context", rs.getString(3));
				mapdata.put("addTime", rs.getString(4));
				mapdata.put("name", rs.getString(5));

				datalist.add(mapdata);
			}
			int index = db.GetInfoCount(sqlCount);
			datas = JSONArray.fromObject(datalist);
			json.put("success", true);
			json.put("datas", datas);
			json.put("results", index);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();

			}
		}
	}

	// 删除
	public void deleteCustom() {
		ConnDB db = new ConnDB();
		Statement ps = null;
		HttpServletRequest req = ServletActionContext.getRequest();
		String id = req.getParameter("customIds");

		String sqld = "";
		id = id.substring(0, id.length() - 1);
		Connection con = db.getConnection();
		try {
			con.setAutoCommit(false);
			ps = con.createStatement();

			boolean bool = false;

			String sql = "delete t_s_customSend  where id in( " + id + " )";
			bool = ps.execute(sql);
			ps = con.createStatement();
			if (bool) {
				// System.out.println("保存成功!!!");
			}
			JSONObject json = new JSONObject();
			json.put("success", true);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			try {
				con.commit();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void setRequest(Map arg0) {
		// TODO Auto-generated method stub
		this.req = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp = arg0;

	}

}
